package com.wxy.gxs.springbootprojectday20.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Aspect
@Component
public class CustomAspect {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    //    切面的作用就是拦截,明确拦截的范围,
//    语法格式:@pointcut("execution()")
    @Pointcut("execution(* com.wxy.gxs.springbootprojectday20.controller.*.*(..))")
    public void log() {


    }

    //    1.方法在执行之前
    @Before("log()")
    public void doBefore(JoinPoint joinPoint) {
//        System.out.println("doBefore------------------");
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        StringBuffer url = request.getRequestURL();
        String ip = request.getRemoteAddr();
        String method = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        ResultLog resultLog = new ResultLog(url.toString(), ip, method, args);


        logger.info("request message : {}",resultLog);

    }

    @After("log()")
    public void doAfoter() {
//        System.out.println("doAfoter-----------------");
        logger.info("doAfoter-----------------");

    }

    @AfterReturning(returning = "result", pointcut = "log()")
    public void resultLog(Object result) {
        logger.info("returning -------- : {}", result);


    }


//    2.方法在执行之后
//    3.方法的返回结果


    //    创建内容类:
    private class ResultLog {
        private String url;
        private String ip;
        private String method;
        private Object[] args;

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getIp() {
            return ip;
        }

        public void setIp(String ip) {
            this.ip = ip;
        }

        public String getMethod() {
            return method;
        }

        public void setMethod(String method) {
            this.method = method;
        }

        public Object[] getArgs() {
            return args;
        }

        public void setArgs(Object[] args) {
            this.args = args;
        }

        public ResultLog(String url, String ip, String method, Object[] args) {
            this.url = url;
            this.ip = ip;
            this.method = method;
            this.args = args;
        }

        @Override
        public String toString() {
            return "ResultLog{" +
                    "url='" + url + '\'' +
                    ", ip='" + ip + '\'' +
                    ", method='" + method + '\'' +
                    ", args=" + Arrays.toString(args) +
                    '}';
        }
    }

}
